package org.finesys.chat.core.base.listener;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.finesys.ai.entity.AigcKnowledge;
import org.finesys.chat.core.base.constants.EventTypeEnum;
import org.finesys.chat.core.base.embedding.factory.KnowledgeStoreFactory;
import org.finesys.chat.core.base.events.KnowledgeStoreEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class KnowledgeStoreListener {

    private final KnowledgeStoreFactory knowledgeStoreFactory;


    /**
     * 知识库事件监听器。
     *
     * @param event 知识库事件对象
     */
    @EventListener
    public void onKnowledgeEvent(KnowledgeStoreEvent event) {
        // 处理知识库模型更新事件
        log.info("KnowledgeStoreEvent received: {}", event);
        // 获取事件类型和聊天模型信息
        String type = event.getType();
        // 获取知识库对象
        AigcKnowledge aigcKnowledge = (AigcKnowledge) event.getSource();
        // 根据事件类型执行相应操作
        EventTypeEnum eventType = EventTypeEnum.getByName(type);
        switch (eventType) {
            case ADD:
            case UPDATE:
                knowledgeStoreFactory.addOrUpdate(aigcKnowledge);
                break;
            case DELETE:
                knowledgeStoreFactory.unregister(aigcKnowledge);
                break;
            default:
                // 处理其他事件类型，例如删除等
                log.warn("Unsupported EventType: {}", event);
        }
    }
}
